/**/
/*防止原始数据被污染*/
let newData = Object.create(data.beautyData);

function sequence(value) {
    return function (obj1, obj2) {
        let v1 = Number(obj1[value]);
        let v2 = Number(obj2[value]);
        if (v1 == v2) {
            return 0;
        } else if (v1 < v2) {
            return -1;
        } else {
            return 1
        }
    }
}

/*销量排序*/
$("#hbf_sales").click(function () {
    newData.sort(sequence("sales"))
    ULObject(newData.reverse())
    ddd()
})
/*价格排序*/
$("#hbf_price").click(function () {
    newData.sort(sequence("goodszprice"))
    ULObject(newData)
    ddd()
})
/*默认*/
$("#hbf_default").click(function () {
    ULObject(data.beautyData);
    ddd()
})
/*搜索*/
let hbfVerify = $(".hbf_verify");
let $1 = $(".hbf_inputBox>input");
$1.focus(function () {
    hbfVerify.css("display", "block");
})
/*查询按钮*/
$("#hbfv_fix").click(function () {
    interregional()
    /*重新绑定事件*/
    ddd()
})
/*清除按钮*/
$("#hbfv_remove").click(function () {
    $1.val("")
})

/*区间查询*/
function interregional() {
    let a1 = Number($1[0].value.trim());
    let a2 = Number($1[1].value.trim());
    let arrD = data.filter(function (v, i) {
        // console.log(v.goodszprice)
        let b = Number(v.goodszprice);
        if (a1 < b && a2 > b) {
            return v;
        }
    })
    /*渲染*/
    // vvv(arrD,ULObject());
    ULObject(arrD)
}

window.onwheel = function () {
    hbfVerify.css("display", "none");
    // $1.val("")
}

/*返回要添加的ul对象、以及要渲染的数据*/
function ULObject(DATA) {
    return ULFLAT ? vvv(DATA, $(".hlw_ul")) : thumbnail(DATA, $(".hlt_ul"));
}

/*对比功能*/
/*初始化*/
ddd()
let chen01 = $(".chen01");
/*对比栏的商品数量*/
let NUM = -1;
/*商品的对比id*/
/*保存对比的商品*/
let ARR = [];

/*初始化*/
function ddd() {
    let hlwLi = $(".hlw_li")
    /*大图片*/
    let boxImg = $(".hlw_boxImg");
    /*切换的盒子*/
    let toggleBox = $(".hlw_toggle");
    let hlwCon = $(".hlw_contrast");
    let index = 0;
    /*小图片的位置*/
    hlwLi.mouseenter(function () {
        /*获取li的下标*/
        index = $(this).index();
        $(toggleBox.get(index)).find(".hlw_toggle_img").off().mouseenter(function () {
            let xiaoImgStc = $(this).find("img").attr("src");
            $(boxImg.get(index)).find("img").attr("src", xiaoImgStc)
        })
    })
    cutover()

    /*对比功能*/
    function cutover() {
        hlwCon.click(function () {
            if ($(this).find(".hgz_choose").attr("class").includes("checked")) {
                $(chen01.find(".tianjiade").get(NUM)).empty();
                $(this).find(".hgz_choose").removeClass(" checked")
                NUM--;
            } else {
                if (NUM >= 3) {
                    NUM = 4;
                    return;
                }
                $(this).find(".hgz_choose").addClass(" checked")
                $("#h_contrastBar").css("display", "block")
                let attr = $(this).find(".hgz_choose").attr("data-choose");
                if (!ARR.includes(attr)) {
                    ARR.push(attr)
                }
                NUM++;
                /*渲染一个商品div*/
                let $li = contrastBarData(queryById($(this).children().attr("data-choose")));
                $(chen01.find(".tianjiade").get(NUM)).empty().append($li);
                removeEvent();

            }
            /*删除全部的对勾*/
            $(".chen02-2").children().click(function () {
                ARR.length = 0
                NUM = -1;
                chen01.find(".tianjiade").empty();
                hlwCon.find(".hgz_choose").removeClass(" checked")
                /*隐藏对比栏*/
                $("#h_contrastBar").css("display", "none")
            })
            /*对比跳转*/
            $("#duibiBtn").off().click(function () {
                let mapID = ""
                chen01.find(".duiBiDelete").each(function (i, v) {
                    mapID += "v" + i + "=" + $(v).attr("data-duibi") + "&";
                })
                location.href = "../html/contrast.html?" + mapID.substring(0, mapID.length - 1);
                ARR.length = 0
            })
        })

        function removeEvent() {
            /*对比的删除按钮*/
            chen01.find(".duiBiDelete").off().click(function () {
                let li = $(this).parents("li");
                let indexLength = li.index();
                let duibiId = li.find(".duiBiDelete").attr("data-duibi");
                ARR.splice(ARR.indexOf(duibiId), 1)
                /*遍历查找要删除的*/
                hlwCon.children().map(function (i, v) {
                    if (v.getAttribute("data-choose") == duibiId) {
                        $(v).removeClass(" checked");
                    }
                })
                NUM--;
                $(".tianjiade").empty();
                ARR.forEach(function (v, i) {
                    $(".tianjiade").get(i).innerHTML = contrastBarData(queryById(v));
                    NUM = ARR.length - 1;
                })
                removeEvent();
            })
        }
    }

    /*商品悬浮事件*/
    $(".hlw_li ").mouseenter(function () {
        // $(this).addClass("hlwbox").siblings().removeClass("hlwbox")
        $(this).attr("transition-style", "in:square:center").siblings().removeAttr("transition-style")
    })
}

/**
 *  通过id查询商品的对象
 * @param goodsId 商品的id
 */
function queryById(goodsId) {
    return dataS.find(function (v, i) {
        return v.goodsid == goodsId;
    })
}

/*返回对比的样式div*/
function contrastBarData(obj) {
    return ` <div class="tianjiade-1">
                                    <a href="../html/goods.html#${obj.goodsid}"><img src="${obj.goodsurls.nowurl}" alt=""></a>
                                </div>
                                <div class="tianjiade-2">
                                    <a href="">${obj.goodstitle}</a>
                                    <div>
                                        <span>¥${obj.goodszprice}</span>
                                        <a href="javascript:void(0)" class="duiBiDelete" data-duibi="${obj.goodsid}">删除</a>
                                    </div>
                                </div>`
}

move()

function move() {
    $(".hlw_join").mouseenter(function () {
        let spId = $(this).attr("data-c");
        let img = $(this).parents("li").children("div").children(":first-child").find("img").attr("src");
        $(this).shoping({
            endElement: ".cxdiw", //结束元素节点，即页面上购物车位置
            iconCSS: "border:0", //飞过的图片样式
            iconImg: img, //飞过的图片信息
            endFunction: function (element) { //动画结束后的回调
                console.log(parseInt($(".cxdsp").html()) + 1)
                $(".cxdsp").html(parseInt($(".cxdsp").html()) + 1);
                /*存储商品的id goodsId = id*/
                localStorage.setItem("goodsId" + spId, spId);
                // let geShu = Number($(".buy-num").val());
                let id = localStorage.getItem(spId)
                /*判断商品的id是否存在*/
                if (id != null) {
                    /*存在进行++*/
                    localStorage.setItem(spId, Number(id) + 1);
                } else {
                    /*商品的id保存*/
                    // spIdS+="goodsId"+ spId;
                    localStorage.setItem("spIdS", localStorage.getItem("spIdS") + "goodsId" + spId + ",");
                    /*不存在初始值*/
                    localStorage.setItem(spId, 1);
                }
                return false;
            }
        });
        // location.reload();
    })
}
